package de.kraenksoft.android.cinemaExample.model;

import java.io.IOException;
import java.net.URL;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class WebTheaterPageParser {
	private URL url;
	private String[] days = new String[7];
	private Document document;
	DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm");
	private String currentYear;
	private boolean foundNewScreenings = false;
	private Theater theater;

	public WebTheaterPageParser(URL url, Theater theater) {
		this.theater = theater;
		this.url = url;
	}

	public void parseMovies() throws IOException, ParseException {
		document = Jsoup.parse(url, 5000);
		parseDays();
		parseScreenings();
	}

	private void parseScreenings() throws ParseException {
		foundNewScreenings = false;
		Elements movieElements = document
				.getElementsByClass("ufcinemaprogram-list-film");
		for (Element movieElement : movieElements) {
			String movieName = movieElement.getElementsByTag("a").get(0).text();
			Movie movie = getMovieForName(movieName);
			parseScreeningsForMovie(movieElement, movie);
		}

	}

	private Movie getMovieForName(String movieName) {
		Movie movie;
		if (theater.containsMovie(movieName)) {
			movie = theater.get(movieName);
		} else {
			movie = new Movie();
			movie.setName(movieName);
			theater.add(movie);
		}
		return movie;
	}

	private void parseScreeningsForMovie(Element movieElement, Movie movie) throws ParseException {
		Elements dayElements = movieElement.siblingElements();
		int dayNumber = 0;
		while (dayNumber < 7) {
			// skip first element that contains the movie name
			Element dayElement = dayElements.get(dayNumber + 1);
			parseScreeningsForDay(dayElement, dayNumber, movie);
			dayNumber++;
		}
	}

	private void parseScreeningsForDay(Element dayElement, int dayNumber,
			Movie movie) throws ParseException {
		Elements timeElements = dayElement.getElementsByTag("a");
		for (Element timeElement : timeElements) {
			String timeString = timeElement.text();
			if (timeString.length() > 2) {
				movie.addScreening(new Screening(dateFormat
						.parse(days[dayNumber] + " " + timeString)));
				foundNewScreenings = true;
			}
		}

	}

	private void parseDays() {
		initializeCurrentYear();
		Elements dayElements = document
				.getElementsByClass("ufcinemaprogram-list-date");
		int i = 0;
		for (Element dayElement : dayElements) {
			days[i] = dayElement.text() + currentYear;
			i++;
		}

	}

	private void initializeCurrentYear() {
		if (currentYear == null) {
			// TODO: This doesn't work in the week of newyears eve!
			Calendar calToday = GregorianCalendar.getInstance();
			currentYear = calToday.get(Calendar.YEAR) + "";
		}
	}

	public boolean foundNewScreenings() {
		return foundNewScreenings;
	}

}
